# -*- coding: utf-8 -*-

from jinja2 import Environment, FileSystemLoader
import odoorpc
import codecs
import re

if __name__ == "__main__":
    """4.生成api"""
    target_models = ['res.archives']# 待生成code的模型
    path = 'd:/temp/'  # 需自定义路径
    for model in target_models:
        snake_name = model.replace('.', '_')
        camel_name = re.sub('_([a-zA-Z])', lambda m: (m.group(1).upper()), snake_name)

        env = Environment(loader=FileSystemLoader('D:/workspace/python/Odoo/odoo16-x64-custom/myaddons/chanke/autocode/templates'))  # 模板路径

        # 生成API resource
        template = env.get_template('api.py')  # 加载模板
        code = template.render(modelName=model, snake_name=snake_name, camel_name=camel_name)
        print(code)
        file_path = path + snake_name + '.py'
        file = codecs.open(file_path, 'w', 'utf-8')
        file.write(code)

    file.close()
